package com.hotel.dao.impl;

import com.hotel.dao.FoodTypeDao;
import com.hotel.pojo.FoodType;
import com.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.util.List;
import java.util.Map;

/**
 * 菜系模块dao实现
 * Created by LinYaTing on 2018/11/23 23:51
 */
public class FoodTypeIDaompl implements FoodTypeDao{

    @Override
    public void save(FoodType foodType) {
        String sql = "insert into foodtype(typeName) values(?)";
        try {
            JdbcUtils.getQueryRunner().update(sql,foodType.getTypeName());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void delete(int id) {
        String sql = "delete from foodtype where id = ?";
        try {
            JdbcUtils.getQueryRunner().update(sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(FoodType foodType) {
        String sql = "update foodtype set typeName = ? where id = ?";
        try {
            JdbcUtils.getQueryRunner().update(sql, foodType.getTypeName(), foodType.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<FoodType> findAll() {
        String sql = "select * from foodtype";
        try {
            return JdbcUtils.getQueryRunner().query(sql, new BeanListHandler<FoodType>(FoodType.class));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public List<FoodType> findList(String typeName) {
        String sql = "select * from foodtype where typeName like ?";
        try {
            return JdbcUtils.getQueryRunner().query(sql,new BeanListHandler<FoodType>(FoodType.class),"%" + typeName + "%");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public FoodType findFoodTypeById(int id) {
        String sql = "select * from foodtype where id = ?";
        try {
            return JdbcUtils.getQueryRunner().query(sql, new BeanHandler<FoodType>(FoodType.class),id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<FoodType> findNameAndId() {
        String sql = "select id,typeName from foodtype";
        try {
            return JdbcUtils.getQueryRunner().query(sql, new BeanListHandler<FoodType>(FoodType.class));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
